package com.ccn.ui.a3project;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ccn.ui.R;
import com.squareup.picasso.Picasso;

public class A3_1_2FeedAdapter extends RecyclerView.Adapter<A3_1_2FeedAdapter.FeedHolder> {

    @NonNull
    @Override
    public FeedHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_feed,parent,false);
        return new FeedHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FeedHolder holder, int position) {
        //用户头像
        Picasso.with(holder.itemView.getContext())
                .load(getAvatarResId(position))
                .centerInside()
                .fit()
                .into(holder.ivAvatar);
        //内容图片
        Picasso.with(holder.itemView.getContext())
                .load(getContentResId(position))
                .centerInside()
                .fit()
                .into(holder.ivContent);

        //nickname
        holder.tvNickName.setText("叮当猫");
    }

    public int getAvatarResId(int position){
        switch (position % 4){
            case 0:
                return R.mipmap.avatar1;
            case 1:
                return R.mipmap.avatar2;
            case 2:
                return R.mipmap.avatar3;
            case 3:
                return R.mipmap.avatar4;
        }
        return 0;
    }

    private int getContentResId(int position){
        switch (position % 4){
            case 0:
                return R.mipmap.meizitu1;
            case 1:
                return R.mipmap.meizitu2;
            case 2:
                return R.mipmap.meizitu3;
            case 3:
                return R.mipmap.meizitu4;
        }
        return 0;
    }

    @Override
    public int getItemCount() {
        return 100;
    }

    public static class  FeedHolder extends RecyclerView.ViewHolder{

        ImageView ivAvatar;
        TextView tvNickName;
        ImageView ivContent;
        public FeedHolder(@NonNull View itemView) {
            super(itemView);
            ivAvatar = itemView.findViewById(R.id.iv_avatar);
            tvNickName = itemView.findViewById(R.id.tv_nickname);
            ivContent = itemView.findViewById(R.id.iv_content);
        }
    }
}
